//
//  LoginPhoneViewController.m
//  insyte
//
//  Created by Vu Dang Tuan on 6/19/15.
//  Copyright (c) 2015 Robert. All rights reserved.
//

#import "LoginPhoneViewController.h"
#import "Helpers.h"
#import "UIColor+InsyteColors.h"
#import "Constants.h"
#import "MBProgressHUD.h"
#import "SBPickerSelector.h"
#import "GlobalObject.h"
#import "SearchPhoneCodeViewController.h"
#import "ServiceManager.h"
#import "VerifyCodeViewController.h"


@interface LoginPhoneViewController ()<UITextFieldDelegate>
@end

@implementation LoginPhoneViewController
{
    NSString* _phoneFromServer;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpNavigationBar];
    [self styleForView];
    [self setupPicker];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    _lbContrycode.text = [NSString stringWithFormat:@"%@ (%@)", [GlobalObject countryName], [[GlobalObject phoneCode] objectForKey:[GlobalObject countryName]]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void) styleForView{
    [Helpers StyleViewWithBorderColor:_viewCode color:[UIColor textfieldBorderColor] corner:0];
    [Helpers StyleButtonWithBorderColor:_btnSendCode color:[UIColor loginButtonColor] corner:0];
    [Helpers StyleViewWithBorderColor:_viewCountryCode color:[UIColor textfieldBorderColor] corner:0];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(dismissKeyboard)];
    
    [self.view addGestureRecognizer:tap];
    _tfCode.delegate = self;
}

- (void)setUpNavigationBar{
    UIBarButtonItem *btnBack = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:Ic_Back] style:UIBarButtonItemStylePlain target:self action:@selector(OnBackClicked)];
    self.navigationItem.leftBarButtonItem = btnBack;
    self.navigationItem.titleView = nil;
}

- (void) OnBackClicked{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)OnSendCodeClicked:(id)sender {
    if ([_tfCode.text isEqualToString:@""]) {
        [_tfCode becomeFirstResponder];
        return;
    }
    
    [ServiceManager registerWithPhone:[[GlobalObject phoneCode] objectForKey:[GlobalObject countryName]] phone:self.tfCode.text cb:^(NSString* rs) {
        if (rs) {
            _phoneFromServer = rs;
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
            hud.mode = MBProgressHUDModeCustomView;
            hud.labelText = @"CODE SENT";
            hud.labelColor = [UIColor loginButtonColor];
            
            hud.color = [UIColor whiteColor];
            hud.customView.layer.borderColor = [UIColor loginButtonColor].CGColor;
            hud.customView.layer.borderWidth = 1;
            UIImageView *imgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:Ic_Small_Email]];
            hud.customView = imgV;
            [hud setDimBackground:YES];
            [hud setOpacity:0.5f];
            [hud hide:YES afterDelay:2];
            [self performSelector:@selector(openEnterCodeScreen) withObject:nil afterDelay:3];
        
        }
    }];
}

- (void)openEnterCodeScreen{
    VerifyCodeViewController *vc = [[VerifyCodeViewController alloc] init];
    vc.phone = _phoneFromServer;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void) setupPicker{
    // picker view
    UITapGestureRecognizer *touchOnPicker = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openPicker)];
    [touchOnPicker setNumberOfTapsRequired:1];
    [touchOnPicker setNumberOfTouchesRequired:1];
    [self.viewCountryCode addGestureRecognizer:touchOnPicker];
}
- (void) openPicker{
    [self.navigationController presentViewController:[[SearchPhoneCodeViewController alloc] init] animated:YES completion:nil];
}

#pragma Textfield delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(void)dismissKeyboard {
    [self.tfCode resignFirstResponder];
}

- (IBAction)OnCheckBoxClicked:(id)sender {
    if (++self.btnCheckbox.tag %2 == 0) {
        [self.btnCheckbox setImage:[UIImage imageNamed:Ic_Check] forState:UIControlStateNormal];
    }else{
        [self.btnCheckbox setImage:[UIImage imageNamed:Ic_Un_Check] forState:UIControlStateNormal];
    }
}



@end
